package kibahed.university.effizientealgorithmen.abstractclasses;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import kibahed.university.effizientealgorithmen.interfaces.IEdge;
import kibahed.university.effizientealgorithmen.interfaces.IVertex;
import kibahed.university.effizientealgorithmen.standardclasses.EdgeWithCosts;

public abstract class AVertex implements IVertex{

	List<IEdge> edgesAsList = new ArrayList<IEdge>();
	IEdge[] edges = null;
	Set<IEdge> edgesAsSet = new HashSet<IEdge>();
	boolean marked = false;
	private int id;
	
	@Override
	public IEdge[] getEdges() {
		if(edges == null){
			edges = new IEdge[edgesAsList.size()];
			for(int i=0;i<edges.length;i++){
				edges[i]=edgesAsList.get(i);
			}
		}
		return edges;
	}

	@Override
	public void add(EdgeWithCosts edge) {
		edgesAsList.add(edge);
		edges=null;
		edgesAsSet.add(edge);
	}
	
	@Override
	public boolean hasEdge(IEdge edge) {
		return edgesAsSet.contains(edge);
	}
	
	public boolean isMarked() {
		return marked;
	}
	
	public void setMarked(boolean marked) {
		this.marked = marked;
	}
	
	public void setID(int id) {
		this.id = id;
	}
	
	public int getID() {
		return id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AVertex other = (AVertex) obj;
		if (id != other.id)
			return false;
		return true;
	}
}
